package com.wuercloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuercloud.entity.QueuesEntity;
import com.wuercloud.service.QueuesService;
import com.wuercloud.utils.PageUtils;
import com.wuercloud.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 队列管理
 * 
 * @author ChinaSun
 * @email hjsw1@126.com
 * @date 2017-03-21 15:15:57
 */
@RestController
@RequestMapping("queues")
@Api(value = "queues", description = "队列管理")
public class QueuesController {
	@Autowired
	private QueuesService queuesService;

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("queues:list")
	@ApiOperation(value = "队列列表信息", notes = "返回队列列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R list(@ApiParam(value = "第几页", required = true) @RequestParam(required = false) Integer page,
			@ApiParam(value = "每页显示几条", required = true) @RequestParam(required = false) Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);

		// 查询列表数据
		List<QueuesEntity> queuesList = queuesService.queryList(map);
		int total = queuesService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(queuesList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 队列列表
	 */
	@GetMapping("/select")
	@RequiresPermissions("queues:select")
	@ApiOperation(value = "队列列表(list)信息", notes = "返回队列列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R select() {
		// 查询列表数据
		List<QueuesEntity> list = queuesService.queryList(new HashMap<String, Object>());

		return R.ok().put("list", list);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{queuesId}")
	@RequiresPermissions("queues:info")
	@ApiOperation(value = "根据id查询队列列表信息", notes = "返回根据id查询的队列列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R info(@ApiParam(value="根据id查询查询对象",required=true)@PathVariable("queuesId") Integer queuesId) {
		QueuesEntity queues = queuesService.queryObject(queuesId);

		return R.ok().put("queues", queues);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("queues:save")
	@ApiOperation(value = "保存队列列表信息", notes = "保存队列列表数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R save(@RequestBody QueuesEntity queues) {
		queuesService.save(queues);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@RequiresPermissions("queues:update")
	@ApiOperation(value = "修改队列列表信息", notes = "修改队列列表数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R update(@RequestBody QueuesEntity queues) {
		queuesService.update(queues);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@RequiresPermissions("queues:delete")
	@ApiOperation(value = "删除队列列表信息", notes = "删除队列列表数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R delete(@RequestBody Integer[] queuesIds) {
		queuesService.deleteBatch(queuesIds);

		return R.ok();
	}

}
